// heapSort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;

void heapifier (int *arr, int i, int size)
{int done=0;
int c,k=0;
int maxChild;
//int temp;
while((i*2<size)&&(!done))
{
	if (i*2==size)
		maxChild=i*2;
	else 
	{if (arr[i*2]>arr[i*2+1])
		maxChild=i*2;
	else maxChild=i*2+1;
	}
	if (arr[i]<arr[maxChild])
	{
	c=arr[i];
	arr[i]=arr[maxChild];
	arr[maxChild]=c;
	i=maxChild;
	}
	else done=1;
}
for(int k=0; k<size;k++)
{
printf("%d  ",arr[k]);
}
printf("\n");
}

void heapsort (int *arr, int size)
{
	int i;
	int c;
	for (i=(size/2)-1;i>=0;i--)
	{
		heapifier(arr,i,size);
	}
	for (i=size-1;i>=1;i--)
	{
	c=arr[0];
	arr[0]=arr[i];
	arr[i]=c;
	heapifier(arr,0,i-1);
	
	}
}
int _tmain(int argc, _TCHAR* argv[])
{int n;
int arr[10];
	printf("vvedite kolichestvo elementov");
	cin>>n;
	srand((unsigned) time(NULL));
	for(int i=0;i<n;i++)
	{	
		arr[i]=rand()%100;
		printf("%d  ", arr[i]);
	}
	printf("\n");
	
heapsort (arr, n);

	
for(int i=0; i<n;i++)
	{	
		printf("%d  ", arr[i]);
	}

	return 0;
}

